package leetcode.offer_two_special.tree;

import leetcode.tree.common.TreeNode;

/**
 * @author mazouri
 * @create 2021-09-12 17:11
 */
public class _50_pathSum_double_recursion {
    int targetSum;
    int count = 0;

    public int pathSum(TreeNode root, int targetSum) {
      if (root == null) return 0;

      this.targetSum = targetSum;
      preorderTraversal(root);
      return count;

    }

    private void preorderTraversal(TreeNode root) {
        if (root!=null){
            dfs(root,targetSum-root.val);
            preorderTraversal(root.left);
            preorderTraversal(root.right);
        }
    }

    private void dfs(TreeNode root, int sum) {
        if (sum == 0) count++;

        if (root.left!=null) dfs(root.left,sum - root.left.val);
        if (root.right!=null) dfs(root.right,sum-root.right.val);
    }
}
